package LeetCode.leetcode349;

import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;

public class Review1 {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            if (!map.containsKey(nums1[i])) {
                map.put(nums1[i], 1);
            }
        }
        for (int i = 0; i < nums2.length; i++) {
            if (map.containsKey(nums2[i])) {
                set.add(nums2[i]);
            }
        }
        int[] res = new int[set.size()];
        AtomicInteger index = new AtomicInteger(0);

        set.forEach(value -> {
            res[index.get()] = value;
            index.compareAndSet(index.get(), index.get() + 1);
        });
        return res;
    }
}
